define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'recruitto/index',
                    add_url: 'recruitto/add',
                    edit_url: 'recruitto/edit',
                    del_url: 'recruitto/del',
                    multi_url: 'recruitto/multi',
                    table: 'recruitTo',
                }
            });

            var table = $("#table");
              table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='source']", form).addClass("selectpage").data("source", "base/source/index").data("primaryKey", "id").data("field", "name",'id').data("params",'{custom: {type: 1}}').data("orderBy", "id desc");
               $("input[name='source']", form).data("params", function (obj) {
                     return {custom: {type:1}};
                });
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'source', title: __('Source'), visible: false,formatter: Table.api.formatter.search},
                        {field: 'source_text', title: __('Source'), operate:false,formatter: Controller.api.formatter.source},
                         {field: 'sheng', title: __('Sheng'),formatter: Controller.api.formatter.province},
                        {field: 'shi', title: __('Shi'),formatter: Controller.api.formatter.shi},
                        {field: 'xian', title: __('Xian'),formatter: Controller.api.formatter.xian},
                        {field: 'address', title: __('详细地址')},
                        {field: 'apply_1', title: __('Apply_1')},
                        {field: 'apply_2', title: __('Apply_2')},
                        {field: 'apply_3', title: __('Apply_3')},
                        {field: 'name', title: __('Name')},
                        {field: 'tel', title: __('Tel')},
                        {field: 'weixin', title: __('Weixin')},
                        {field: 'email', title: __('Email')},
                        {field: 'info', title: __('Info')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        contract_index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'recruitto/contract_index',
                    add_url: 'recruitto/add',
                    del_url: 'recruitto/contract_del',
                    multi_url: 'recruitto/multi',
                    table: 'recruitTo',
                }
            });

            var table = $("#table");
              table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='source']", form).addClass("selectpage").data("source", "base/source/index").data("primaryKey", "id").data("field", "name",'id').data("params",'{custom: {type: 1}}').data("orderBy", "id desc");
               $("input[name='source']", form).data("params", function (obj) {
                     return {custom: {type:1}};
                });
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                queryParams: function (params) {
                    //这里可以追加搜索条件
                    var filter = params.filter ? JSON.parse(params.filter) : {}; //判断当前是否还有其他高级搜索栏的条件
                    var op = params.op ? JSON.parse(params.op) : {}; 
                    //这里可以动态赋值，比如从URL中获取admin_id的值，filter.admin_id=Fast.api.query('admin_id');
                    filter.status = 2;
                    op.status = "=";
                  
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    return params;
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                         {field: 'name', title: __('Name')},
                        {field: 'contract_name', title: __('协议所填姓名')},
                        {field: 'url', title: __('头像'), formatter: Controller.api.formatter.avatar},
                        {field: 'contract_url', title: __('协议链接'), align: 'left', formatter: Controller.api.formatter.url},
                        {field: 'source', title: '表单来源', visible: false,formatter: Table.api.formatter.search},
                        {field: 'source_text', title: '表单来源', operate:false,formatter: Controller.api.formatter.source},
                        {field: 'sheng', title: __('Sheng'),formatter: Controller.api.formatter.province},
                        {field: 'shi', title: __('Shi'),formatter: Controller.api.formatter.shi},
                        {field: 'xian', title: __('Xian'),formatter: Controller.api.formatter.xian},
                        {field: 'address', title: __('详细地址')},

                        {field: 'tel', title: __('Tel')},
                        {field: 'weixin', title: __('Weixin')},
                        {field: 'email', title: __('Email')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
          formatter: {//渲染的方法
            avatar: function (value, row, index) {
                  return '<img width="45" height="45" src="'+value+'" class="img-circle">';
                },
                url: function (value, row, index) {
                    return '<div class="input-group input-group-sm" style="width:250px;"><input type="text" class="form-control input-sm" value="' + value + '"><span class="input-group-btn input-group-sm"><a href="' + value + '" target="_blank" class="btn btn-default btn-sm"><i class="fa fa-link"></i></a></span></div>';
                },
                province: function (value, row, index) {
                    var areatext = '';
                    if(value){
                        areatext = Config.arealist[value];
                    }
                  return areatext;
                },
                shi: function (value, row, index) {
                    var areatext = '';
                    if(value){
                        areatext = Config.arealist[value];
                       
                    }
                  return areatext;
                },
                xian: function (value, row, index) {
                    var areatext = '';
                    if(value){
                        areatext = Config.arealist[value];
                       
                    }
                  return areatext;
                },
                source: function (value, row, index) {
                    var sourcetext = '';
                    if(row['source']){
                        sourcetext = Config.SourceList[row['source']];
                    }
                  return sourcetext;
                },
               
            }
        }
    };
    return Controller;
});